package com.isoftstone.merchant.controller.system.menu;


import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.common.Tidings;
import com.isoftstone.common.entity.SysMenu;
import com.isoftstone.common.service.ISysMenuService;
import com.isoftstone.common.service.ISysRoleMenuService;
import com.isoftstone.common.service.ISysUserRoleService;
import com.isoftstone.merchant.service.role.IPRoleUserService;
import com.isoftstone.util.Const;
import com.isoftstone.util.PageData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@Qualifier(value = "menu")
public class MenuController extends BaseController {

    @Autowired
    ISysMenuService sysMenuService;

    @Autowired
    ISysRoleMenuService sysRoleMenuService;

    @Autowired
    ISysUserRoleService sysUserRoleService;

    @Autowired
    IPRoleUserService pRoleUserService;


    /**
     * 
      * @return
     */

    @RequestMapping("/system/menu/getmerchantmenu")
    @ResponseBody
    public Object getMenuPage() {
        Tidings<List<SysMenu>>  td =new Tidings<List<SysMenu>>();
        ModelAndView md = new ModelAndView();
        PageData pd = this.getPageData();
        //page.setPd(pd);
        try {
            List<SysMenu> menuList = sysMenuService.getMenuAll();
            // md.addObject("menuList", menuList);
            td.setT(menuList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //md.setViewName("system/menu/menu_list");
        return td;
    }


    /**
     * 查询header链接
     * @return
     */
    @RequestMapping("/system/menu/getMenuByUrl")
    @ResponseBody
    public Object getMenuByUrl() {
        PageData pd = this.getPageData();
        String menuId = pd.getString("menuId");
        Tidings<List<SysMenu>> tidings = new Tidings<List<SysMenu>>();
        try {
            List<SysMenu> menuList = null;

            /**
             *  01 商户端 menu
             */

            if(this.getUser() !=null&& StringUtils.equals(this.getUser().getSystem(),Const.SystemCode.CODE_MMS)){

                menuList = sysMenuService.getMenuByUrl(this.getUser(),menuId);
            }if(this.getUser() !=null&& StringUtils.equals(this.getUser().getSystem(),Const.SystemCode.CODE_BMS)){


                /**
                 * 银行端
                 */

                menuList = sysMenuService.YgetMenuByUrl(this.getUser(),menuId);
            }
            tidings.setStatus(Const.SUCCESS);
            tidings.setT(menuList);
            return tidings;
        } catch (Exception e) {
            tidings.setStatus(Const.ERROR);
            tidings.setMsg("数据异常！");
            e.printStackTrace();
        }
        return tidings;
    }

}

